package com.itbupt.eat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itbupt.eat.entity.Employee;
import com.itbupt.eat.mapper.EmployeeMapper;
import com.itbupt.eat.service.IEmployeeService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * @Author BUPT-Dingchang
 * @Create 2024/4/20 21:39
 * ClassName: IEmployeeServiceImpl
 * Description:
 */

@Service
public class IEmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 分页查询+条件查询
     * @param currentPage   当前页
     * @param pageSize    每页显示的条数
     * @param name      员工姓名
     * @return  Page<Employee>
     */
    @Override
    public Page<Employee> getPage(Long currentPage, Long pageSize, String name) {

        // 1.创建新的条件查询构造器，用于根据员工姓名搜索
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();

        // 2.添加过滤条件：根据员工姓名查询
        lqw.like(Strings.isNotEmpty(name), Employee::getName, name);

        // 3.添加排序条件：按最后修改时间降序排序
        lqw.orderByDesc(Employee::getUpdateTime);

        // 4.分页构造器
        Page<Employee> page = new Page<>(currentPage, pageSize);

        // 5.把分页和条件查询包装器传入数据层的selectPage()方法
        return employeeMapper.selectPage(page, lqw);

    }


    /**
     * 修改员工信息
     * @param request 请求对象
     * @param employee  员工对象
     * @return  Boolean
     */
    @Override
    public Boolean update(HttpServletRequest request, Employee employee) {

        // 1. 设置修改人
        employee.setUpdateUser((Long) request.getSession().getAttribute("employee"));

        // 2. 设置修改时间
        employee.setUpdateTime(LocalDateTime.now());

        // 3. 调用数据层的更新操作
        return updateById(employee);
    }
}
